#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifndef ONLINE_JUDGE
#define dbg(x...) do { cout << "\033[32;1m" << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) { for (auto v: a) cout << v << ' '; err(x...); }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#else
#define dbg(...)
#endif

const int N = 1e6+50;

int t, n;

LL a[N], b[N], c[N], sc[N];

int main(int argc, char const *argv[])
{
	scanf("%d", &t);
	for (int kk=0; kk<t; ++kk) {
		scanf("%d", &n);
		for (int i=1; i<=n; ++i) {
			scanf("%lld%lld", &a[i], &b[i]);
			a[i+n] = a[i];
			b[i+n] = b[i];
		}

		for (int i=1; i<=2*n; ++i) {
			c[i] = max(0LL, a[i]-b[i-1]);
		}
		for (int i=1; i<=2*n; ++i)
			sc[i] = sc[i-1] + c[i];

		LL ans = 1e18;
		for (int i=1; i<=n; ++i) {
			ans = min(ans, a[i]+sc[i+n-1]-sc[i]);
			// dbg(a[i], sc[i+n-1], sc[i]);
		}
		printf("%lld\n", ans);

	}

    return 0;
}

